package com.huaweicloud.haydn.delivercore.agent.service.metadata.impl;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.springframework.stereotype.Service;

import com.huaweicloud.haydn.delivercore.agent.common.exception.BusinessErrorEnum;
import com.huaweicloud.haydn.delivercore.agent.common.exception.BusinessException;
import com.huaweicloud.haydn.delivercore.agent.dto.excel.FieldEmptyExcelDto;

import lombok.extern.slf4j.Slf4j;

/**
 * 功能描述
 *
 * @since 2023-01-06
 */
@Slf4j
@Service("dws")
public class DwsDatabaseUtil extends BaseDatabaseUtil {
    @Override
    public String getSelectedDatabasesSql(String dbNames) throws BusinessException {
        return null;
    }

    @Override
    public String getAllTablesByDbSql(String dbNames) {
        return "select table_schema,table_name from information_schema.tables where table_schema in "+dbNames+" order by table_name";
    }

    @Override
    public String getSelectedTableSql(String dbNames, String tableNames) {
        return null;
    }

    @Override
    public String getAllFieldsSql() {
        return "SELECT * FROM pg_get_tabledef(?)";
    }

    @Override
    public String getAllFieldsByDbSql(String dbNames) {
        return null;
    }

    @Override
    public String getAllFieldsSql(String dbNames, String tableNames) {
        return null;
    }

    @Override
    public String getDriverClass() {
        return "org.postgresql.Driver";
    }

    @Override
    public String getAllDatabasesSql() throws BusinessException {
        return "select datname from pg_database where datistemplate is false";
    }

    @Override
    public String getAllSchemasSql() {
        return "select schema_name from information_schema.schemata where schema_owner != 'Ruby' OR schema_name in ('public','scheduler')";
    }

    @Override
    public String getAllTablesSql() {
        return "select table_name from information_schema.tables where table_schema = ? order by table_name";
    }

    @Override
    public String getURL(String hostname, String port, String databaseName) {
        return "jdbc:postgresql://" + hostname + ":" + port + "/" + databaseName;
    }

    @Override
    public String getURLException(String hostname, String port, String databaseName) {
        return "jdbc:postgresql://" + hostname + ":" + port + "/" + databaseName;
    }

    @Override
    public Connection getConnection() throws BusinessException {
        Properties props = new Properties();
        props.setProperty("user", getUsername());
        props.setProperty("password", getPassword());
        props.setProperty("ssl", "true");
        // 获取数据库连接URL
        String url = "";
        try {
            url = getURL(getHostname(), getPort(), getDatabaseName());
            // 加载驱动
            Class.forName(getDriverClass());
            // 创建数据库连接
            return DriverManager.getConnection(url, props);
        } catch (ClassNotFoundException | SQLException e) {
            log.error("getConnection end. database load driver failed.", e);
            throw new BusinessException(BusinessErrorEnum.BASE_INTERNAL_SERVER_ERROR);
        }
    }

    @Override
    public List<FieldEmptyExcelDto> getAllEmptyRateFields(String datalinkName, String dbNames, String schemas, Map<String, List<String>> fieldTableMap) {
        return null;
    }
}
